
//-----------------------------------------------------------------------------
//  
//  Package
//  
//-----------------------------------------------------------------------------

package
{
	
	//-------------------------------------------------------------------------
	//  
	//  Imports
	//  
	//-------------------------------------------------------------------------
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	//-------------------------------------------------------------------------
	//  
	//  Class
	//  
	//-------------------------------------------------------------------------
	
	/**
	 * Provides a set methods to read and write file data as plain text or 
	 * byte data. These methods match the shell methods found in the source of 
	 * Tamarin Central.
	 */
	public class FileUtil
	{
		/**
		 * Constructs a FileUtil object. Cannot be used as it will throw an error.
		 */
		public function FileUtil()
		{
			throw new ArgumentError(getQualifiedClassName(this) + "$ class cannot be instantiated.", 2012);
		}
		
		/**
		 * Checks to see if the file exists.
		 * 
		 * @param filename The name of the file.
		 * @return <code>true</code> if the file exists, <code>false</code> otherwise.
		 */
		public static function exists(filename:String):Boolean
		{
			var file:File = new File(filename);
			
			return file.exists;
		}
		
		[Deprecated(replacement="FileUtil.readBytes")]
		public static function fileToByteArray(filename:String, readOnly:Boolean = false):ByteArray
		{
			return readBytes(filename);
		}
		
		public static function read(filename:String):String
		{
			var file:File = new File(filename);
			
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var data:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
			fileStream.close();
			
			return data;
		}
		
		public static function readBytes(filename:String):ByteArray
		{
			var bytes:ByteArray = new ByteArray();
			
			var file:File = new File(filename);
			
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			fileStream.readBytes(bytes);
			fileStream.close();
			
			return bytes;
		}
		
		public static function write(filename:String, data:String):void
		{
			var file:File = new File(filename);
			
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeUTFBytes(data);
			fileStream.close();
		}
		
		[Deprecated(replacement="FileUtil.writeBytes")]
		public static function writeByteArray(filename:String, bytes:ByteArray):Boolean
		{
			try
			{
				writeBytes(filename, bytes);
			}
			catch (error:Error)
			{
				return false;
			}
			
			return true;
		}
		
		public static function writeBytes(filename:String, bytes:ByteArray):void
		{
			var file:File = new File(filename);
			
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(bytes);
			fileStream.close();
		}
	}
}